//
//  YSGNurseAppointmentInfoCell.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/3/7.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit
import Kingfisher

protocol YSGNurseAppointmentInfoCellDelegate: NSObjectProtocol {
    func YSGNurseAppointmentInfoCellClick( didSelected model: YSGNurseAppointmentInfoModel)
}

class YSGNurseAppointmentInfoCell: UITableViewCell,YSGDatePickerDelege {

    var viewModel: YSGNurseAppointmentInfoViewModel?
    var model : YSGNurseAppointmentInfoModel?
    weak var delegate: YSGNurseAppointmentInfoCellDelegate?
    
    
    var lineLabel: UILabel!{
        return bodyBgView.viewWithTag(104) as? UILabel
    }
    
    var titleLabel: UILabel!{
        return bodyBgView.viewWithTag(102) as? UILabel
    }
    
    var inputTextField: UITextField!{
        return bodyBgView.viewWithTag(103) as? UITextField
    }
    
    var bodyLabel: UILabel!{
        return bodyBgView.viewWithTag(101) as? UILabel
    }
    
    var bodyBgView: UIView!{
        return self.contentView.viewWithTag(100)
    }
    
    var tipLabel: UILabel!{
        return self.contentView.viewWithTag(105) as? UILabel
    }
    
    var selectButton : UIButton!{
        return self.contentView.viewWithTag(1013) as? UIButton
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }

    func config(_model : YSGNurseAppointmentInfoModel){
        model = _model
        titleLabel.text = _model.title
        inputTextField?.placeholder = _model.placeHolder
        inputTextField?.text = model?.value
        inputTextField?.addTarget(self, action: #selector(textfieldDidEnd(_:)), for: .editingDidEnd)
        switch _model.style{
        case .lastStyle:
            lineLabel.isHidden = true
        case .tipStyle:
            lineLabel.isHidden = true
            tipLabel.isHidden = false
        default :
            lineLabel.isHidden = false
        }
        if (bodyLabel != nil){
            bodyLabel.text = _model.value
        }
        
        selectButton?.addTarget(self, action: #selector(selectAction), for: .touchUpInside)
        
        if ((viewModel?.familyModelArray.count)! != 0 && tipLabel != nil) {
            tipLabel.text = String(format: "查到您在医院存在%d张就诊卡，请选择", (viewModel?.familyModelArray.count)!)
        }
    
        
        setRadiusView(_style: _model.style!)

        
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    
    func setRadiusView(_style : YSGNurseAppointmentInfoStyle){
        let radius: CGFloat = 12;
        // 圆角位置
        var corner: UIRectCorner = [.topLeft, .topRight,.bottomLeft,.bottomRight]
        switch _style {
        case .singelStyle:
            corner = [.topLeft, .topRight,.bottomLeft,.bottomRight]
        case .firstStyle:
            corner = [.topLeft, .topRight]
        case .lastStyle:
            corner = [.bottomLeft,.bottomRight]
        case .tipStyle:
            corner = [.topLeft, .topRight,.bottomLeft,.bottomRight]
        case .normolStyle:
            break
        }
        
        let rect = CGRect(x: 0, y: 0, width: (screenW-35), height: bodyBgView.height)
        let path: UIBezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners: corner, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer: CAShapeLayer = CAShapeLayer()
        maskLayer.frame = rect;
        maskLayer.path = path.cgPath
        bodyBgView.layer.mask = maskLayer;
    }
    
    
    
 @objc func selectAction() {
        
        let picker = UINib.loadViewFrom(CommonPickerView.self)
        picker.frame = CGRect(x: 0, y: 0, width: screenW, height: screenH)


        
        if model?.code == "patientId"{
            delegate?.YSGNurseAppointmentInfoCellClick(didSelected: self.model!)
            
        }else{

            picker.selectArray = model?.selectArray
            picker.show(type: .nurseAppointment)
            picker.selectedClosure = {[weak self] selectModel in
                self?.model?.value = selectModel.name
                self?.bodyLabel.text = selectModel.name
                self?.viewModel?.dataSource[self?.model?.code ?? ""] = selectModel.code
                
            }
        }

    }
    
    
    @IBAction func timeSelectAction(_ sender: UIButton) {
        showDatePicker()
    }
    @objc func textfieldDidEnd(_ sender: UITextField) {
        if sender.text?.count == 0 {
            viewModel?.dataSource.removeValue(forKey: (model?.code)!)
        }else{

            if model?.code == "adress"{
                viewModel?.dataSource[(model?.code)!] = (viewModel?.mapModel?.district ?? "") + (viewModel?.mapModel?.name ?? "") + sender.text!
            }else{
                viewModel?.dataSource[(model?.code)!] =  sender.text
            }
        }
        
        model?.value = sender.text ?? ""
    }
    
    
    /// 时间日期选择器
    func showDatePicker() {
        let picker = UINib.loadViewFrom(YSGDatePicker.self)
        picker.dateStr = ""
        picker.delegate = self
        UIApplication.shared.keyWindow?.addSubview(picker)
    }
    
    // MARK: - YSGDatePickerDelege 选择检查时间代理
    func YSGDatePickerConfirmBtnAction(dateStr: String) {
       // viewModel?.model.applyCheckTime = dateStr
        bodyLabel.text = dateStr
        viewModel?.dataSource[model?.code ?? ""] = dateStr
    }
}
